import {EventEmitter, Injectable} from '@angular/core';
import {Http, RequestOptions} from "@angular/http";
import MTSConstants from "../config/config";
import {BackCode} from "../model/backCode";

@Injectable()
export class CamRecordService {
  servicename:string="CamRecordService";
  searchEvent: EventEmitter<string> = new EventEmitter();  //车辆管理列表搜索标志
  searchFreshEvent: EventEmitter<string> = new EventEmitter();  //搜索框刷新标志
  listFreshEvent: EventEmitter<string> = new EventEmitter();   //列表刷新标志
  constructor(private http:Http) { }

  //根据车辆，查询所有用车记录
  getCamApplyInfoByVehicle(pageNumber:number,pageSize:number,userId:string,vehicleoid:string,status:string){
    const url = `${MTSConstants.HOSTUser}applications/vehicle?pageNumber=${pageNumber}&pageSize=${pageSize}&userId=${userId}&vehicleoid=${vehicleoid}&exclude=${status}`;
    //const url = `${MTSConstants.HOSTUser}applications/vehicle?pageNumber=${pageNumber}&pageSize=${pageSize}&exclude=2,3,4&datelimit=y`;
    return this.http.get(url)
      .toPromise()
      .then(res=>{return res.json()})
      .catch((error: any) => {return this.handleError('getCamApplyInfoByVehicle',error);});
  }


  //删除维护类型信息
  deleteApplication(oid:string): Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}application/${oid}`;
    //let body = JSON.stringify(val);//这个也可以
    const headers = MTSConstants.headers;//spring的restful接口用这个
    const options = new RequestOptions({ headers: headers });
    return this.http.delete(url, options)
      .toPromise()
      //.then((res) =>{console.log(res)})
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('deleteApplication',error);});
  }


  handleError(functionName:string,error: any): Promise<any> {
    console.error(`服务 : ${this.servicename} , 函数 : ${functionName} 。发生错误 : `, error);
    return Promise.reject(error.message || error);
  }
}
